﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Himp.TaskScheduling
{
    /// <summary>
    /// 煤样取样任务
    /// </summary>
    public class SampleObtainTaskService : TaskSchedulingAppService, ISampleObtainTaskService
    {
        private IRepository<SampleTestTask, long> _sampleTestTaskRepository;

        public SampleObtainTaskService(IRepository<SampleTestTask, long> sampleTestTaskRepository)
        {
            _sampleTestTaskRepository = sampleTestTaskRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<SampleTestTaskDto>> GetListAsync(SampleTestTaskInput input)
        {
            var iQuery = await _sampleTestTaskRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.SampleCode) && !string.IsNullOrEmpty(input.SampleType),
                i => i.SampleCode == input.SampleCode && i.SampleType == input.SampleType);
            iQuery = iQuery.Where(i => i.ManualFlag == 1);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<SampleTestTask>, List<SampleTestTaskDto>>(iQuery.ToList());

                return new PagedResultDto<SampleTestTaskDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<SampleTestTask>, List<SampleTestTaskDto>>(items.ToList());

                return new PagedResultDto<SampleTestTaskDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 下发取样化验任务
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task PostAsync(SampleObtainTaskCreateDto input)
        {
            #region 20240921

            //SampleTestTask sampleTestTask = new SampleTestTask("C092001"
            //    , "T092001Z"
            //    , "2024092008350122"
            //    , "3"
            //    , 2
            //    , "Mad,A"
            //    , 1);

            //SampleTestTask sampleTestTask = new SampleTestTask("C092008"
            //    , "T092008Z"
            //    , "2024092013080122"
            //    , "3"
            //    , 2
            //    , "Mad,A"
            //    , 1);

            //SampleTestTask sampleTestTask = new SampleTestTask("C092003"
            //    , "T092003Z"
            //    , "2024092009440122"
            //    , "3"
            //    , 2
            //    , "Mad,A"
            //    , 1);

            //SampleTestTask sampleTestTask = new SampleTestTask("C092009"
            //    , "T092009Z"
            //    , "2024092014240122"
            //    , "3"
            //    , 2
            //    , "Mad,A"
            //    , 1);

            //SampleTestTask sampleTestTask = new SampleTestTask("C092005"
            //    , "T092005Z"
            //    , "2024092010510122"
            //    , "3"
            //    , 2
            //    , "Mad,A"
            //    , 1);

            #endregion

            #region 20240922

            //SampleTestTask sampleTestTask = new SampleTestTask("C092412"
            //    , "T092412Z"
            //    , "2024092314580122"
            //    , "3"
            //    , 2
            //    , "Mad,A"
            //    , 1);

            //sampleTestTask = await _sampleTestTaskRepository.InsertAsync(sampleTestTask);

            //sampleTestTask = new SampleTestTask("C092410"
            //   , "T092410Z"
            //   , "2024092313420122"
            //   , "3"
            //   , 2
            //   , "Mad,A"
            //   , 1);
            //sampleTestTask = await _sampleTestTaskRepository.InsertAsync(sampleTestTask);

            //sampleTestTask = new SampleTestTask("C092408"
            //   , "T092408Z"
            //   , "2024092312290122"
            //   , "3"
            //   , 2
            //   , "Mad,A"
            //   , 1);
            //sampleTestTask = await _sampleTestTaskRepository.InsertAsync(sampleTestTask);

            //sampleTestTask = new SampleTestTask("C092407"
            //   , "T092407Z"
            //   , "2024092311490122"
            //   , "3"
            //   , 2
            //   , "Mad,A"
            //   , 1);
            //sampleTestTask = await _sampleTestTaskRepository.InsertAsync(sampleTestTask);

            //sampleTestTask = new SampleTestTask("C092402"
            //   , "T092402Z"
            //   , "2024092308570122"
            //   , "3"
            //   , 2
            //   , "Mad,A"
            //   , 1);

            #endregion

            #region 20240926

            SampleTestTask sampleTestTask = new SampleTestTask("C092612"
                , "T092612Z"
                , "2024092514460122"
                , "3"
                , 2
                , "Mad,A"
                , 1);

            sampleTestTask = await _sampleTestTaskRepository.InsertAsync(sampleTestTask);

            sampleTestTask = new SampleTestTask("C092610"
               , "T092610Z"
               , "2024092513340122"
               , "3"
               , 2
               , "Mad,A"
               , 1);
            sampleTestTask = await _sampleTestTaskRepository.InsertAsync(sampleTestTask);

            sampleTestTask = new SampleTestTask("C092608"
               , "T092608Z"
               , "2024092512120122"
               , "3"
               , 2
               , "Mad,A"
               , 1);
            sampleTestTask = await _sampleTestTaskRepository.InsertAsync(sampleTestTask);

            sampleTestTask = new SampleTestTask("C092607"
               , "T092607Z"
               , "2024092511400122"
               , "3"
               , 2
               , "Mad,A"
               , 1);
            sampleTestTask = await _sampleTestTaskRepository.InsertAsync(sampleTestTask);

            sampleTestTask = new SampleTestTask("C092602"
               , "T092602Z"
               , "2024092508510122"
               , "3"
               , 2
               , "Mad,A"
               , 1);

            #endregion

            sampleTestTask = await _sampleTestTaskRepository.InsertAsync(sampleTestTask);
        }
    }
}
